package com.ksd.modules.download.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ksd.common.annotation.ApiLog;
import com.ksd.common.controller.BaseController;
import com.ksd.common.core.result.R;
import com.ksd.common.po.BatchIdsPO;
import com.ksd.common.web.annotation.RepeatSubmit;
import com.ksd.modules.download.po.AdminDownloadsUserBuySavePO;
import com.ksd.modules.download.po.AdminDownloadsUserBuySearchPO;
import com.ksd.modules.download.service.IAdminDownloadsUserBuyService;
import com.ksd.modules.download.vo.AdminDownloadsUserBuyVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
public class AdminDownloadsUserBuyController extends BaseController {

    @Autowired
    private IAdminDownloadsUserBuyService adminDownloadsUserBuyService;

    /**
     * 【下载资源】-【订单】- 列表
     * @return
     */
    @PostMapping("/download/order/list")
    public R<Page<AdminDownloadsUserBuyVo>> findAdminDownloadss(@Validated @RequestBody AdminDownloadsUserBuySearchPO adminDownloadsCategorySearchPO){
        return R.data(adminDownloadsUserBuyService.findAdminDownloadsUserBuys(adminDownloadsCategorySearchPO));
    }


    /**
     * 【下载资源】-【订单】- 保存和更新
     * @param adminDownloadsCategorySavePO
     * @return
     */
    @PostMapping("/download/order/saveupdate")
    @ApiLog("【下载资源】-【订单】- 保存和更新")
    @RepeatSubmit
    public R<AdminDownloadsUserBuyVo> saveUpdateAdminDownloads(@Validated @RequestBody AdminDownloadsUserBuySavePO adminDownloadsCategorySavePO){
        return R.data(adminDownloadsUserBuyService.saveUpdateAdminDownloadsUserBuy(adminDownloadsCategorySavePO));
    }

    /**
     * 【下载资源】-【订单】- 根据id查询明细
     * @param id
     * @return
     */
    @PostMapping("/download/order/get/{id}")
    public R<AdminDownloadsUserBuyVo> getAdminDownloadsById(@PathVariable("id")Long id){
        return R.data(adminDownloadsUserBuyService.getAdminDownloadsUserBuyById(id));
    }

    /**
     * 【下载资源】-【订单】- 批量彻底删除
     *
     * @return
     */
    @PostMapping("/download/order/batch/remove")
    @ApiLog("【下载资源】-【订单】- 批量物理删除")
    @RepeatSubmit
    public R<Boolean> batchRemovesAdminDownloads(@RequestBody BatchIdsPO batchIdsPO) {
        return R.data(adminDownloadsUserBuyService.batchRemovesAdminDownloadsUserBuy(batchIdsPO));
    }

    /**
     * 【下载资源】-【订单】- 单个物理删除
     * @param id
     * @return
     */
    @PostMapping("/download/order/remove/{id}")
    @ApiLog("【下载资源】-【订单】- 单个物理删除")
    public R<Boolean> removeAdminDownloadsById(@PathVariable("id")Long id){
        return R.data(adminDownloadsUserBuyService.removeAdminDownloadsUserBuyById(id));
    }

    /**
     * 【下载资源】-【订单】- 修改状态
     *
     * @return
     */
    @PostMapping("/download/order/status/{id}/{field}")
    @ApiLog("【下载资源】-【订单】- 修改状态")
    @RepeatSubmit
    public R<Boolean> updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @RequestParam("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return R.data(adminDownloadsUserBuyService.updateStatus(id, field, Integer.parseInt(value)));
        }else{
            return R.data(adminDownloadsUserBuyService.updateStatusStr(id, field, String.valueOf(value)));
        }
    }

    /**
     * 【下载】-【订单】- 绑定订单
     *
     * @return
     */
    @PostMapping("/download/order/userbind")
    @ApiLog("【小册】-【订单】- 绑定订单")
    @RepeatSubmit
    public R<Boolean> bindUserOrder(
            @RequestParam("uuid")String uuid,
            @RequestParam("opid") Long opid
    ) {
        return R.data(adminDownloadsUserBuyService.bindUserOrder(uuid, opid));
    }
}
